package drools;

import com.alibaba.fastjson.JSON;
import com.irdstudio.rsp.drools.beans.RuleBean;
import com.irdstudio.rsp.drools.beans.RuleBean.RESULT;
import com.irdstudio.rsp.drools.common.BizRuleAgendaFilter;
import junit.framework.TestCase;
import org.apache.commons.io.IOUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;
import java.util.Set;

public abstract class RuleTestBase extends TestCase {

    Logger log = LoggerFactory.getLogger(this.getClass());

    RuleBean param ;

    BizRuleAgendaFilter filter;

    public void before() throws IOException {
        InputStream jsonInput = this.getClass().getResourceAsStream(this.getRuleId() + ".json");
        assertNotNull(jsonInput);
        String json = IOUtils.toString(jsonInput, "UTF-8");
        param = JSON.parseObject(json, RuleBean.class);
        assertNotNull(param);
    }

    public abstract String getRuleId();

    public void after() {
        assertNotNull(filter);
        // 处理结果
        Set<String> acceptSet = new LinkedHashSet<String>(filter.getRuleSet());
        // 取差集 得到执行通过的规则
        acceptSet.removeAll(param.getRefuseSet());
        for (String ruleId : acceptSet) {
            param.setResult(ruleId, RESULT.ACCEPT, "通过");
        }

        log.info("fireRule result: \n{}", JSON.toJSONString(param, true));
    }
}
